package com.jy.reservations.service.impl;

import java.util.List;

import com.jy.books.domain.Books;
import com.jy.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;

import org.springframework.transaction.annotation.Transactional;
import com.jy.reservations.mapper.ReservationsMapper;
import com.jy.reservations.domain.Reservations;
import com.jy.reservations.service.IReservationsService;

/**
 * 图书预留Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-05-30
 */
@Service
public class ReservationsServiceImpl implements IReservationsService 
{
    @Autowired
    private ReservationsMapper reservationsMapper;

    /**
     * 查询图书预留
     * 
     * @param reservationId 图书预留主键
     * @return 图书预留
     */
    @Override
    public Reservations selectReservationsByReservationId(Long reservationId)
    {
        return reservationsMapper.selectReservationsByReservationId(reservationId);
    }

    /**
     * 查询图书预留列表
     * 
     * @param reservations 图书预留
     * @return 图书预留
     */
    @Override
    public List<Reservations> selectReservationsList(Reservations reservations)
    {
        return reservationsMapper.selectReservationsList(reservations);
    }

    /**
     * 新增图书预留
     * 
     * @param reservations 图书预留
     * @return 结果
     */
    @Transactional
    @Override
    public int insertReservations(Reservations reservations)
    {
        int rows = reservationsMapper.insertReservations(reservations);
        insertBooks(reservations);
        return rows;
    }

    /**
     * 修改图书预留
     * 
     * @param reservations 图书预留
     * @return 结果
     */
    @Transactional
    @Override
    public int updateReservations(Reservations reservations)
    {
        reservationsMapper.deleteBooksByBookId(reservations.getReservationId());
        insertBooks(reservations);
        return reservationsMapper.updateReservations(reservations);
    }

    /**
     * 批量删除图书预留
     * 
     * @param reservationIds 需要删除的图书预留主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteReservationsByReservationIds(Long[] reservationIds)
    {
        reservationsMapper.deleteBooksByBookIds(reservationIds);
        return reservationsMapper.deleteReservationsByReservationIds(reservationIds);
    }

    /**
     * 删除图书预留信息
     * 
     * @param reservationId 图书预留主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteReservationsByReservationId(Long reservationId)
    {
        reservationsMapper.deleteBooksByBookId(reservationId);
        return reservationsMapper.deleteReservationsByReservationId(reservationId);
    }

    /**
     * 新增图书信息信息
     * 
     * @param reservations 图书预留对象
     */
    public void insertBooks(Reservations reservations)
    {
        List<Books> booksList = reservations.getBooksList();
        Long reservationId = reservations.getReservationId();
        if (StringUtils.isNotNull(booksList))
        {
            List<Books> list = new ArrayList<Books>();
            for (Books books : booksList)
            {
                books.setBookId(reservationId);
                list.add(books);
            }
            if (list.size() > 0)
            {
                reservationsMapper.batchBooks(list);
            }
        }
    }
}
